package net.xdclass.component;



import lombok.extern.slf4j.Slf4j;
import net.xdclass.config.SmsConfig;
import net.xdclass.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
public class SmsComponent {

    /**
     * 发送短信地址
     */
    private static final String URL_TEMPLATE = "https://jmsms.market.alicloudapi.com/sms/send?mobile=%s&templateId=%s&value=%s";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SmsConfig smsConfig;

    /**
     * 发送短信验证码
     * @param to
     * @param templateId
     * @param value
     */
    public void send(String to, String templateId, String value) {

       long beginTime =  CommonUtil.getCurrentTimestamp();

        //构建发送的url
        //format:格式化字符串，包含了一些占位符（例如 %s, %d, %f 等），这些占位符会被后续传入的参数替代。
        String url = String.format(URL_TEMPLATE, to, templateId, value);
        HttpHeaders headers = new HttpHeaders();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.set("Authorization", "APPCODE " + smsConfig.getAppCode());
        //构建请求对象
        HttpEntity entity = new HttpEntity(headers);
        //url 请求方式 返回的内容
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        long endTime =  CommonUtil.getCurrentTimestamp();
        log.info("耗时={},url={},body={}" ,endTime-beginTime,url,response.getBody());
        if (response.getStatusCode().is2xxSuccessful()){
            log.info("短信验证码发送成功");
        }else {
            log.info("短信验证码发送失败",response.getBody());
        }
    }
}
